package com.example.mpbile2032021606170hsy0512homework;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import androidx.appcompat.widget.Toolbar;

import com.example.mpbile2032021606170hsy0512homework.fragment.ETCFragment;
import com.example.mpbile2032021606170hsy0512homework.fragment.EnviFragment;
import com.example.mpbile2032021606170hsy0512homework.fragment.IndexFragment;
import com.example.mpbile2032021606170hsy0512homework.fragment.LightFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private FrameLayout frameLayout;
    private NavigationView navigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name){
            @Override
            public  void  onDrawerOpened(View drawerView){
                super.onDrawerOpened(drawerView);
            }
            @Override
            public  void  onDrawerClosed(View drawerView){
                super.onDrawerClosed(drawerView);
            }
        };
        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, IndexFragment.newInstance("",""),"")
                .commit();
        toolbar.setTitle("智能交通系统");
         navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
             @Override
             public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                 item.setCheckable(true);
                 switch (item.getItemId()){
                     case  R.id.itemIndex:
                         getSupportFragmentManager().beginTransaction()
                                 .replace(R.id.framelayout,IndexFragment.newInstance("",""),"")
                                 .commit();
                         toolbar.setTitle("智能交通系统");
                         break;
                     case  R.id.itemETC:
                         getSupportFragmentManager().beginTransaction()
                                 .replace(R.id.framelayout, ETCFragment.newInstance("",""),"")
                                 .commit();
                         toolbar.setTitle("ETC账户");
                         break;
                     case  R.id. itemEnvi:
                         getSupportFragmentManager().beginTransaction()
                                 .replace(R.id.framelayout, EnviFragment.newInstance("",""),"")
                                 .commit();
                         toolbar.setTitle("环境指标");
                         break;
                     case R.id.itemLight:
                         getSupportFragmentManager().beginTransaction()
                                 .replace(R.id.framelayout, LightFragment.newInstance("",""),"")
                                 .commit();
                         toolbar.setTitle("交通灯");
                         break;
                     case  R.id.itemExit:
                         MainActivity.this.finish();
                         break;
                 }
                 drawerLayout.closeDrawers();
                 return  true;
             }
         });
    }
    private  void  initView(){
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);
        navigationView = (NavigationView) findViewById(R.id.navigation);
    }
}